﻿Imports TBCC.UtilityMobile
Public Class TimeSetRtiForm

    '起始地址
    Private _offset As UShort = 0

    Private _offset_Time As UShort = 10   '20100727更新系统时钟校准时，读取基准时间地址
    Private buf1() As UShort
    '选择设备的网络地址
    Private _netID As Byte
    Private result As Integer
    Private _ReadTime As Boolean
    Private g_netID_Time As Integer = 255

    '时间参数数据
    Private _buf() As Short
    Private bufTime() As Short

    Private _pi As ProjectItem

    Private Sub TimeSetRtiForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        _pi = Me.MyParentForm.PI

        EnableModify(False)
        Me.btnModify.Enabled = False
        Me.Btn_SetTime.Enabled = False
        Me.btnShowDevTime.Enabled = False


        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(False)
        End If
        Timer1.Start() ' LoadData()
    End Sub


    Private Function LoadData() As Boolean

        Try
            _buf = COMHelper.Instance.Send4Short(g_netID_Time, FunctionCode.WordReadonly, _offset_Time, 7) '读1号子控时间 _offset_Time20100727更新系统

        Catch ex As Exception

        End Try

        If _buf Is Nothing Then
            msg.ShowError(g_strReadError)

            Me.btnModify.Enabled = False

            _ReadTime = False
            ' msg.ShowError("读取参数失败")

            Return False


        ElseIf _buf(0) < 0 OrElse _buf(1) < 0 OrElse _buf(2) < 0 OrElse _buf(3) < 0 OrElse _buf(4) < 0 OrElse _buf(5) < 0 Then
            Me.btnModify.Enabled = False
            Me.btnShowDevTime.Enabled = False

            _ReadTime = False
            Return False
            '  

        Else
            _ReadTime = True

        End If

        Me.btnModify.Enabled = True
        Me.btnShowDevTime.Enabled = True
        Me.Btn_SetTime.Enabled = True

        Try
            Me.nudYear.Value = _buf(0) + 1900
            Me.nudMonth.Value = _buf(1)
            Me.nudDay.Value = _buf(2)
            Me.nudHour.Value = _buf(3)
            Me.nudMinute.Value = _buf(4)
            Me.nudSecond.Value = _buf(5)
        Catch ex As Exception
            msg.ShowError(ex.Message)
        End Try
        Return True

    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub



    Private Sub EnableModify(ByVal value As Boolean)
        ' Me.btnModify.Enabled = Not value
        Me.btnOk.Enabled = value
        Me.btnShowDevTime.Enabled = Not value

        Me.nudYear.ReadOnly = Not value
        Me.nudMonth.ReadOnly = Not value
        Me.nudDay.ReadOnly = Not value
        Me.nudHour.ReadOnly = Not value
        Me.nudMinute.ReadOnly = Not value
        Me.nudSecond.ReadOnly = Not value

        Dim i As Integer
        If value Then
            i = 1
        Else
            i = 0
        End If

        Me.nudYear.Increment = i
        Me.nudMonth.Increment = i
        Me.nudDay.Increment = i
        Me.nudHour.Increment = i
        Me.nudMinute.Increment = i
        Me.nudSecond.Increment = i

    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        If _pi.LocalRunStatus = LocalRunStatus.Start Then
            msg.ShowError("终端运行未停止，无法修改时间。")
            Exit Sub
        End If

        Me.GroupBox2.Text = "设置时间"
        Me.btnModify.Enabled = False
        Me.Btn_SetTime.Enabled = False
        EnableModify(True)
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        
        Me.TableLayoutPanel1.Enabled = False



        Dim result As Boolean
        Me.Cursor = Cursors.WaitCursor
        result = Save(1)
        Me.TableLayoutPanel1.Enabled = True
        Me.Cursor = Cursors.Default

        If result Then
            msg.Show("保存成功")
            LoadData()
            Me.btnModify.Enabled = True
            Me.Btn_SetTime.Enabled = True
        Else
            Exit Sub
        End If

        Me.GroupBox2.Text = "基准时钟时间"
        EnableModify(False)
    End Sub

    ''' <summary>
    ''' 保存数据
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save(ByVal valueEn As Integer) As Boolean
        'Dim result As Boolean

        Select Case _pi.ProjectType
            Case ProjectType_Enum.Car '移动车载
                result = Write(255, valueEn)
            Case ProjectType_Enum.Box '小批零
                result = Write(255, valueEn)
                'result = Write(2) And result '暂时不支持2号设备
            Case Else
                Exit Function
        End Select

        If result = False Then
            msg.ShowError("修改时间失败。")
        End If

        Return result


    End Function

    Private Function ReadAllDevTime() As String

        ReadAllDevTime = String.Empty
        Dim str As String = Nothing
        Dim netcount As Integer
        Dim str_Time As String = Nothing

        Select Case _pi.ProjectType
            Case ProjectType_Enum.Car
                netcount = 1
            Case ProjectType_Enum.Box
                netcount = 1 '暂时不支持2号设备
        End Select

        Try

            '读取基准时钟时间

            ' _buf = COMHelper.Instance.Send4Short(g_netID, FunctionCode.WordReadonly, _offset_Time, 7) '读1号子控时间 _offset_Time20100727更新系统

            str_Time = _ReadDevTimeS(255)   '20100727修改读取基准时钟时间
            If str_Time IsNot Nothing Then
                ReadAllDevTime &= str_Time & vbCrLf
            Else
                ReadAllDevTime &= "基准时钟时间：" & g_strReadError & vbCrLf
                ' msg.ShowError(g_strReadError)
                '  Return Nothing
            End If



            For netid As Integer = 1 To netcount
                str = _ReadDevTime(netid)
                If str IsNot Nothing Then
                    ReadAllDevTime &= str & vbCrLf
                Else
                    ReadAllDevTime &= netid & "号设备系统时间：" & g_strReadError & vbCrLf
                End If
            Next
        Catch ex As Exception
            ReadAllDevTime = String.Empty
            msg.Show(ex.Message)
        End Try

    End Function
    Private Function _ReadDevTimeS(ByVal netID As Integer) As String
        _ReadDevTimeS = String.Empty

        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = COMHelper.Instance.Send4Short(netID, FunctionCode.WordReadonly, _offset_Time, 7) '读1号子控时间
            If _buf Is Nothing Then
                'Me.nudYear.Minimum = 0
                'Me.nudYear.Value = 0
                'Me.btnModify.Enabled = False
                _ReadTime = False
                Return Nothing
            Else
                _ReadTime = True
            End If
        End If

        bufTime = _buf


        _ReadDevTimeS &= " 基准时钟系统时间："



        Me.nudYear.Value = _buf(0) + 1900
        Me.nudMonth.Value = _buf(1)
        Me.nudDay.Value = _buf(2)
        Me.nudHour.Value = _buf(3)
        Me.nudMinute.Value = _buf(4)
        Me.nudSecond.Value = _buf(5)


        _ReadDevTimeS &= String.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", _
            _buf(0) + 1900, _buf(1), _buf(2), _buf(3), _buf(4), _buf(5))

    End Function
    Private Function _ReadDevTime(ByVal netID As Integer) As String
        _ReadDevTime = String.Empty

        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = __ReadDevTime(netID)
            If _buf Is Nothing Then
                Me.nudYear.Minimum = 0
                Me.nudYear.Value = 0
                'Me.btnModify.Enabled = False
                Return Nothing
            End If
        End If

        If netID = 255 Then
            _ReadDevTime &= " 主设备系统时间："
        Else
            _ReadDevTime &= netID & "号设备系统时间："
        End If

        If netID = g_netID_Time Then
            Me.nudYear.Value = _buf(0) + 1900
            Me.nudMonth.Value = _buf(1)
            Me.nudDay.Value = _buf(2)
            Me.nudHour.Value = _buf(3)
            Me.nudMinute.Value = _buf(4)
            Me.nudSecond.Value = _buf(5)
        End If

        _ReadDevTime &= String.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", _
            _buf(0) + 1900, _buf(1), _buf(2), _buf(3), _buf(4), _buf(5))

    End Function
    Private Function __ReadDevTime(ByVal netID As Integer) As Short()

        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = COMHelper.Instance.Send4Short(netID, FunctionCode.WordReadonly, _offset, 7) '读1号子控时间
        End If

        Return _buf
    End Function

    Private Function Write(ByVal netid As Integer, ByVal valueEn As Integer) As Boolean
        Dim value As Byte

        Dim result As Boolean = False
        If valueEn = 1 Then
            value = Me.nudYear.Value - 1900
            If Not Write(netid, 0, value) Then
                GoTo WriteERROR
            End If
            Sleep()

            value = Me.nudMonth.Value
            If Not Write(netid, 1, value) Then
                GoTo WriteERROR
            End If
            Sleep()

            value = Me.nudDay.Value
            If Not Write(netid, 2, value) Then
                GoTo WriteERROR
            End If
            Sleep()

            value = Me.nudHour.Value
            If Not Write(netid, 3, value) Then
                GoTo WriteERROR
            End If
            Sleep()

            value = Me.nudMinute.Value
            If Not Write(netid, 4, value) Then
                GoTo WriteERROR
            End If
            Sleep()

            value = Me.nudSecond.Value
            If Not Write(netid, 5, value) Then
                GoTo WriteERROR
            End If
        End If
       
        Sleep()

        'value = Me.nudWeek.Value
        'COMHelper.Instance.Broadcast(functionCode, 6, value)
        'sleep()

        '修改使能位
        value = valueEn
        If Not Write(netid, 7, value) Then
            GoTo WriteERROR
        End If

        GoTo WriteSUCCESS

WriteERROR:
        Return False

WriteSUCCESS:
        Return True

    End Function

    Private Function Write(ByVal netid As Byte, ByVal offset As UShort, ByVal value As Byte) As Boolean
        Return COMHelper.Instance.Write(netid, FunctionCode.WordWrite, offset, value) '非广播地址
    End Function

    Sub Sleep()
        System.Threading.Thread.Sleep(100)
    End Sub

    Private Sub nudYear_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudYear.ValueChanged

    End Sub

    Private Sub nudMonth_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudMonth.ValueChanged

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()

        If LoadData() Then
        Else
            ' msg.ShowWarning("读取数据失败！")
        End If
        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(True)
        End If
    End Sub

    Private Sub btnShowDevTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowDevTime.Click
        sender.Enabled = False
        Me.btnModify.Enabled = False
        Dim str As String
        str = ReadAllDevTime()
        If str.Length > 0 Then
            msg.Show(str)
        End If
        sender.Enabled = True
        Me.btnModify.Enabled = True
    End Sub

    Private Sub Btn_SetTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_SetTime.Click

        If _pi.LocalRunStatus = LocalRunStatus.Start Then
            msg.ShowError("终端运行未停止，无法修改时间。")
            Exit Sub
        End If
        Me.btnModify.Enabled = False
        Me.Btn_SetTime.Enabled = False
        Dim str As String
        Dim result As Boolean
        Me.Cursor = Cursors.WaitCursor
        result = Save(2)
        Me.Cursor = Cursors.Default

        If result Then
            msg.Show("强制修改成功！")

            str = ReadAllDevTime()
            If str.Length > 0 Then
                msg.Show(str)
            End If

            Me.btnModify.Enabled = True
            Me.Btn_SetTime.Enabled = True
            EnableModify(False)
        Else
            Me.btnModify.Enabled = True
            Me.Btn_SetTime.Enabled = True
            EnableModify(False)
            Exit Sub
        End If
    End Sub
End Class